{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 动态条形图的一种改进"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### 1. 初始设置\n",
    "\n",
    "首先，导入所需的库。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入所需的库\n",
    "import pandas as pd\n",
    "import akshare as ak"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 定义数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 当需要更新数据时，去掉注释后运行一次\n",
    "\n",
    "# 从 akshare 获取历史数据，速度比较慢\n",
    "df_all_history = ak.covid_19_history()\n",
    "\n",
    "# 保存到 Excel 文件中，以便下次快速读取数据\n",
    "df_all_history.to_excel('./data/covid_history.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 你可以前往 https://github.com/linjiwx/mp 下载画图用的数据和完整代码\n",
    "df_all = pd.read_excel('./data/covid_history.xlsx', index_col=0).fillna('')\n",
    "\n",
    "# 将字符串格式的日期转换为日期格式\n",
    "df_all['date'] = pd.to_datetime(df_all['date'])\n",
    "\n",
    "# 将时间格式转为字符串格式的日期，以 YYYY-mm-dd 的形式保存到另外一列\n",
    "df_all['dates'] = df_all['date'].apply(lambda x:x.strftime('%Y-%m-%d'))\n",
    "\n",
    "# 添加现存确诊列\n",
    "df_all['current'] = df_all['confirmed'] - df_all['cured'] - df_all['dead']\n",
    "\n",
    "# 排除省份的数据，只保留国家的数据，设置日期为索引\n",
    "dfc = df_all.query(\"province=='' and country!='钻石公主号邮轮'\").set_index('date')\n",
    "\n",
    "# 用透视表整理数据格式\n",
    "df_pivot = dfc.pivot_table(values='confirmed', index='country', columns='dates').fillna(0)\n",
    "\n",
    "# 根据国家代码，插入国旗图片的链接\n",
    "df_country = pd.read_excel('./data/国家代码.xlsx', index_col=0)\n",
    "covid_pivot = df_country.join(df_pivot, how='right').fillna(0)\n",
    "image_url = ['https://www.countryflags.io/'+str(i)+'/flat/64.png' for i in covid_pivot.代码]\n",
    "covid_pivot.insert(1, '图片链接', image_url)\n",
    "\n",
    "# 删除没用的列\n",
    "covid_pivot = covid_pivot.drop(columns = ['代码'])\n",
    "\n",
    "# 按列排序\n",
    "covid_pivot = covid_pivot.sort_values(by=['2020-07-24'], ascending=False)\n",
    "\n",
    "# 保存到 Excel 文件\n",
    "covid_pivot.to_excel('./data/covid_pivot.xlsx')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 开始作图\n",
    "\n",
    "借助第三方工具，分三步完成：\n",
    "\n",
    "（1）注册并登录 https://app.flourish.studio/ \n",
    "\n",
    "（2）在浏览器中访问 https://public.flourish.studio/visualisation/3280860/\n",
    "\n",
    "（3）点击右上角的 「Duplicate and edit」，然后就可以自己随意调整数据和显示效果。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
